package com.briup.bms.service.impl;

import com.briup.bms.bean.SCustomer;
import com.briup.bms.bean.SProduct;
import com.briup.bms.common.exceptions.BmsServiceException;
import com.briup.bms.common.utils.ResultCode;
import com.briup.bms.dao.SCustomerMapper;
import com.briup.bms.dao.SItemMapper;
import com.briup.bms.dao.SProductMapper;
import com.briup.bms.service.SProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author: Paocha
 * @date: 2021-11-23-15:50
 */
@Service
public class ProductServiceImpl implements SProductService {

    private final SProductMapper productMapper;
    private final SItemMapper itemMapper;

    public ProductServiceImpl(SProductMapper productMapper, SItemMapper itemMapper) {
        this.productMapper = productMapper;
        this.itemMapper = itemMapper;
    }

    @Override
    public PageInfo<SProduct> findByConditon(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        name = (name==null||"".equals(name))?name:"%"+name+"%";
        List<SProduct> products=productMapper.selectByCondition(name);
        return new PageInfo<>(products);
    }

    @Override
    public PageInfo<SProduct> findByPageHelper(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SProduct> products=productMapper.selectAll();
        return new PageInfo<>(products);
    }

    @Override
    public void saveOrUpdate(SProduct product) {
        if(product.getId()!=null) {
            //更新
            productMapper.updateByPrimaryKey(product);
        }else {
            //保存
            productMapper.insert(product);
        }
    }

    @Override
    public void deleteCustomer(Integer productIds) {
        SProduct product=productMapper.selectByPrimaryKey(productIds);
        if (ObjectUtils.isEmpty(product)){
            throw new BmsServiceException(ResultCode.DATA_NOT_EXIST,"数据不存在");
        }
        itemMapper.deleteByPrimaryKey(null,null,productIds);
        productMapper.deleteByPrimaryKey(productIds);
    }


    @Override
    public void batchDeleteCustomer(Set<Integer> productIds) {
        productMapper.batchDeleteCustomer(productIds);
    }


}
